package org.wx.thread.thread;

import io.netty.util.concurrent.DefaultEventExecutor;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.Promise;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @description Promise 对Future 类自动进行结果执行
 * @author: wangxun
 * @createDate: 2021/11/26
 * @version: 1.0
 */
@Slf4j
public class PromiseTest {
    @SneakyThrows
    public static void main(String[] args) {
        DefaultEventExecutor executor = new DefaultEventExecutor();
        Promise<Object> promise = executor.newPromise();
        // 添加执行
        promise.addListener( future -> {
            log.info("msg:{}",future.get());
        });
//        executor.execute(()->{
//            try {
//                Thread.sleep(1000);
//
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
//        promise.setSuccess("Hello");


        FutureTask futureTask = new FutureTask<String>(()->{
            Thread.sleep(4000);
            return "111";
        });
        executor.submit(futureTask);
        // 将返回值结果传递
        promise.setSuccess(futureTask.get());

        executor.shutdown();
        executor.awaitTermination(1, TimeUnit.HOURS);

    }
}
